In [ ]:
import pandas as pd
import numpy as np

Resampling

documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html

For arguments to 'freq' parameter, please see Offset Aliases

create a date range to use as an index

In [ ]:
# min: minutes
my_index = pd.date_range('9/1/2016', periods=9, freq='min')

In [ ]:

create a time series that includes a simple pattern

In [ ]:
my_series = pd.Series(np.arange(9), index=my_index)

In [ ]:

Downsample the series into 3 minute bins and sum the values of the timestamps falling into a bin


In [ ]:
my_series.resample('3min').sum()

Downsample the series into 3 minute bins as above, but label each bin using the right edge instead of the left

Notice the difference in the time indices; the sum in each bin is the same


In [ ]:
my_series.resample('3min', label='right').sum()

Downsample the series into 3 minute bins as above, but close the right side of the bin interval

"count backwards" from end of time series


In [ ]:
my_series.resample('3min', label='right', closed='right').sum()

Upsample the series into 30 second bins

asfreq()


In [ ]:
#select first 5 rows 
my_series.resample('30S').asfreq()[0:5]
define a custom function to use with resampling

In [ ]:
def custom_arithmetic(array_like):
    temp = 3 * np.sum(array_like) + 5
    return temp
apply custom resampling function

In [ ]:
my_series.resample('3min').apply(custom_arithmetic)

In [ ]: